package com.applink.sell_proof.dialog;

import android.app.Dialog;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.applink.sell_proof.R;

public class EAlertDialog extends DialogFragment implements
		android.view.View.OnClickListener {
	public static final String TAG = "WaitingDialog";
	private String mTitle;
	private String mNegative;
	private String mPositive;
	private String mMessage;
	private OnClickListener mOnClickListener;

	public EAlertDialog(OnClickListener listener, String title,
			String negative, String positive, String message) {
		mTitle = title;
		mNegative = negative;
		mPositive = positive;
		mMessage = message;
		mOnClickListener = listener;
		setCancelable(false);
		setStyle(STYLE_NO_TITLE, 0);
	}

	// there is only negative button
	public EAlertDialog(OnClickListener listener, String title,
			String navigative, String message) {
		this(listener, title, navigative, null, message);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View layout = null;
		if (mPositive == null) {
			layout = inflater.inflate(R.layout.alert_dialog_negative, null);
		} else {
			layout = inflater.inflate(R.layout.alert_dialog, null);
			layout.findViewById(R.id.btn_positive).setOnClickListener(this);
			((TextView) layout.findViewById(R.id.btn_positive))
					.setText(mPositive);
		}
		layout.findViewById(R.id.btn_negative).setOnClickListener(this);
		((TextView) layout.findViewById(R.id.btn_negative)).setText(mNegative);
		((TextView) layout.findViewById(R.id.dialog_title)).setText(mTitle);
		((TextView) layout.findViewById(R.id.message)).setText(mMessage);
		return layout;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		return super.onCreateDialog(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_negative:
			if (mOnClickListener != null) {
				mOnClickListener.onClick(getDialog(), Dialog.BUTTON_NEGATIVE);
			} else {
				dismiss();
			}
			break;
		case R.id.btn_positive:
			if (mOnClickListener != null) {
				mOnClickListener.onClick(getDialog(), Dialog.BUTTON_POSITIVE);
			} else {
				dismiss();
			}
		default:
			break;
		}
	}
}